package com.fengycx.exception;

import com.fengycx.constants.GlobalExceptionCode;
import com.fengycx.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;


/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 捕捉Exception异常，返回统一的错误提示
    @ExceptionHandler(value = Exception.class)
    public R<Void> exceptionErrorMethod(Exception e, HttpServletRequest request) {
        e.printStackTrace();
        //返回友好
        if("NotLoginException".equals(e.getClass().getSimpleName())){
            return R.error(GlobalExceptionCode.LOGIN_ERROR);
        }
        if("NotPermissionException".equals(e.getClass().getSimpleName())){
            return R.error( GlobalExceptionCode.NO_PERMISSION);
        }
        log.error("发生异常错误，请求地址：{}，异常信息{}", request.getRequestURI(), e.getMessage());
        return R.error(GlobalExceptionCode.SERVICE_ERROR);
    }


    // 捕捉GlobalException异常，把错误日志抛给用户 
    @ExceptionHandler(value = GlobalException.class)
    public R globalCustomMethod(GlobalException e, HttpServletRequest request) {
        e.printStackTrace();
        log.error("发生异常错误，请求地址：{}，异常信息：{}", request.getRequestURI(), e.getMessage());
        return R.error(e.getCode(), e.getMsg());
    }
}

